/*
-------------------------------------------------

	File.cpp

	Dmitriy Kuzmenko (Dgut)

-------------------------------------------------
*/

#include "File.h"
#include <string.h>

namespace Autumn
{

File::File( void )
{
}

File::~File( void )
{
}

const bool File::Init( void )
{
#if defined IPHONE
	[ [ [ NSBundle mainBundle] resourcePath ] getCString : resDirectory
											   maxLength : sizeof( resDirectory ) - 1
												encoding : NSASCIIStringEncoding ];
	
	[ [ NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES ) objectAtIndex : 0 ]
											 getCString : docDirectory
											 maxLength : sizeof( docDirectory ) - 1
											 encoding : NSASCIIStringEncoding ];
	
	const_cast< bool & >( IPAD ) = [ [ UIDevice currentDevice ].model rangeOfString : @"iPad" ].location != NSNotFound;
#elif defined WIN32
	strcpy( resDirectory, "." );
	strcpy( docDirectory, "." );
#endif

	return true;
}

void File::Shutdown( void )
{
}

FILE * const File::Open( const char * name, const char * mode )
{
	char temp[ 256 ];
	FILE * stream;

	sprintf( temp, "%s/%s", docDirectory, name );
	if( stream = fopen( temp, mode ) )
		return stream;

	if( *mode == 'w' )
		return 0;

	sprintf( temp, "%s/%s", resDirectory, name );
	return fopen( temp, mode );
}

}	// namespace Autumn